package com.cyt.sys.service.impl;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.sys.dao.SysRoleRuleDao;
import com.cyt.sys.model.po.SysRoleRule;
import com.cyt.sys.model.vo.SysRoleRuleVo;
import com.cyt.sys.service.SysRoleRuleService;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 角色数据权限service实现类
 *
 * @author shengQiang yu
 * @date 2019-03-22 10:04
 */
@Service("baseRoleRuleService")
public class SysRoleRuleServiceImpl extends BaseServiceImpl<SysRoleRule> implements SysRoleRuleService {

    @Resource
    private SysRoleRuleDao sysRoleRuleDao;

    @Autowired
    public void setBaseDao(SysRoleRuleDao sysRoleRuleDao) {
        this.baseDao = sysRoleRuleDao;
    }

    /**
     * 根据角色id删除
     *
     * @param roleId 角色Id
     * @return
     * @author shengQiang yu
     * @date 2019-03-22 15:48
     */
    @Override
    public int deleteByRoleId(String roleId){
       return sysRoleRuleDao.deleteByRoleId(roleId);
    }

    /**
     * 添加或修改
     *
     * @param sysRoleRuleVo 对象
     * @return
     * @author shengQiang yu
     * @date 2019-03-22 10:07
     */
    @Override
    public ReturnResult addOrUpdate(SysRoleRuleVo sysRoleRuleVo) {
        //先删除对象
        sysRoleRuleDao.deleteByRoleId(sysRoleRuleVo.getRoleId());
        if(StringUtil.isNotEmpty(sysRoleRuleVo.getRuleIds())){
            String[] ids= sysRoleRuleVo.getRuleIds().split(",");
            for(String id:ids){
                sysRoleRuleDao.insert(new SysRoleRule(sysRoleRuleVo.getRoleId(),id));
            }
        }
        return new ReturnResult(BaseConstant.DATABASE_SUCCEED_MSG);
    }
}
